package cn.edu.neusoft.model.bean;

import cn.edu.neusoft.util.ImageDownloader;
import lombok.Data;

import java.io.IOException;

@Data
public class CarPeccancy {
    private String id;
    private String devId;
    private int devChnnum;
    private String devName;
    private String devChnname;
    private String carNum;
    private int carNumtype;
    private String carNumTypeStr;
    private int carNumcolor;
    private String carNumcolorStr;
    private int carSpeed;
    private int carType;
    private String carTypeStr;
    private int carColor;
    private String carColorStr;
    private int carLength;
    private String carDirect;
    private String carWayCode;
    private String carWayCodeStr;
    private int capTime;
    private String capTimeStr;
    private long capDate;
    private String capDateStr;
    private String infNote;
    private int maxSpeed;
    private int minSpeed;
    private String carImgUrl;
    private String carImg1Url;
    private String carImg2Url;
    private String carImg3Url;
    private String carImg4Url;
    private String carImg5Url;
    private int carImgPlateBottom;
    private int carImgPlateRight;
    private int carImgPlateLeft;
    private int carImgPlateTop;
    private int dcCleanFlag;
    private int bindStat;
    private Integer carImgCount;
    private int recStat;
    private String devChnid;
    private int saveFlag;
    private String carNumPic;
    private String combinedPicUrl;
    private String verifyMemo;
    private int recType;
    private String recTypeStr;
    private int proStat;
    private int cloStat;
    private int trsfmark;

    public void setCombinedPicUrl(String strCombinedPicUrl) {
        // 检查是否为无效URL（如"0"、null、空字符串等）
        if (strCombinedPicUrl == null || strCombinedPicUrl.isEmpty() || 
            strCombinedPicUrl.equals("0") || strCombinedPicUrl.equals("null")) {
            // 当combinedPicUrl为空时，尝试使用其他图片字段
            String alternativeUrl = findAlternativeImageUrl();
            if (alternativeUrl != null) {
                System.out.println("[INFO] Using alternative image URL: " + alternativeUrl);
                // 使用替代URL进行图片处理，并存入combinedPicUrl字段
                processImageAndSetCombinedPicUrl(alternativeUrl);
                return;
            } else {
                this.combinedPicUrl = strCombinedPicUrl; // 保持原值
                return;
            }
        }
        
        // 如果combinedPicUrl不为空，正常处理
        processImageAndSetCombinedPicUrl(strCombinedPicUrl);
    }
    
    private void processImageAndSetCombinedPicUrl(String imageUrl) {
        // 找到字符串中'd'的位置
        int indexOfD = imageUrl.indexOf("d");
        
        if (indexOfD == -1) {
            System.err.println("[ERROR] 'd' character not found, cannot process URL: " + imageUrl);
            this.combinedPicUrl = imageUrl; // 保持原值
            return;
        }
        
        // 截取从'd'开始到字符串末尾的部分
        String substringAfterD = imageUrl.substring(indexOfD);
        
        // 拼接前缀和后缀
        String tempUrl = "https://172.17.20.106/evo-pic/" + substringAfterD + "?oss_addr=172.17.20.106:8081";

        //下载到本地
        try {
            this.combinedPicUrl = ImageDownloader.processImage(tempUrl, true); // 启用日志输出
        } catch (IOException e) {
            System.err.println("[ERROR] ==> CarPeccancy image download failed <==");
            System.err.println("[ERROR] Original URL: " + imageUrl);
            System.err.println("[ERROR] Processing URL: " + tempUrl);
            System.err.println("[ERROR] Exception message: " + e.getMessage());
            e.printStackTrace();
            // 发生异常时保持原始URL
            this.combinedPicUrl = imageUrl;
        }
    }

    private String findAlternativeImageUrl() {
        if (this.carImgUrl != null && !this.carImgUrl.isEmpty()) {
            return this.carImgUrl;
        }
        if (this.carImg1Url != null && !this.carImg1Url.isEmpty()) {
            return this.carImg1Url;
        }
        if (this.carImg2Url != null && !this.carImg2Url.isEmpty()) {
            return this.carImg2Url;
        }
        if (this.carImg3Url != null && !this.carImg3Url.isEmpty()) {
            return this.carImg3Url;
        }
        if (this.carImg4Url != null && !this.carImg4Url.isEmpty()) {
            return this.carImg4Url;
        }
        if (this.carImg5Url != null && !this.carImg5Url.isEmpty()) {
            return this.carImg5Url;
        }
        return null;
    }
}